package com.cy.mall.service.message.impl;

import com.cy.mall.comm.annotation.Message;
import com.cy.mall.comm.constant.Constants;
import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.model.dto.message.OrderStatusNotificationMessageDTO;
import com.cy.mall.model.dto.wechat.WechatMessageTemplateDTO;
import com.cy.mall.model.dto.wechat.WechatMessageTemplateDataDTO;
import com.cy.mall.model.enums.MessageType;
import com.cy.mall.model.enums.wechat.WechatMessageTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 订单授信通过（云科额度审批通过）信息发送 service
 * @Author tanyilin
 * @Date 2019/4/1 15:01
 */
@Slf4j
@Message(type = MessageType.ORDER_DECISION_PASS)
@Service
public class OrderDecisionPassMessageImpl extends MessageServiceImpl {

    /**
     * 真正执行发送消息的方法
     *
     * @param message
     */
    @Override
    public void execute(String message) {
        //订单第三方唯一编号 thirdLoanId
        String thirdLoanId = message;
        //查询发送微信消息的订单相关参数
        OrderStatusNotificationMessageDTO template = orderMapper.selectOrderStatusNotificationMessageDTO(thirdLoanId);
        if(template != null && !"dev".equals(Constants.PROFILES_ACTIVE)) {
            //发送微信消息
            sendWechatMessageTemplate(template);
        }
    }

    /**
     * 发送微信消息模板
     * @param template
     * @return void
     */
    private void sendWechatMessageTemplate(OrderStatusNotificationMessageDTO template) {

        if(template.getOpenId() == null) {
            log.info("订单[orderId={}，loanId={}]状态更新，由于客户没有绑定微信，无法发送微信消息。", template.getOrderId(), template.getThirdLoanId());
            return;
        }

        //封装参数map
        WechatMessageTemplateDataDTO first = new WechatMessageTemplateDataDTO();
        first.setValue("恭喜您，您申请的家居分期授信已通过。");

        //授信额度
        WechatMessageTemplateDataDTO keyword1 = new WechatMessageTemplateDataDTO();
        keyword1.setValue("￥" + template.getDecisionAmount());

        //授信时间
        WechatMessageTemplateDataDTO keyword2 = new WechatMessageTemplateDataDTO();
        keyword2.setValue(CommonUtils.dateFormat2(template.getDecisionTime()));

        WechatMessageTemplateDataDTO remark = new WechatMessageTemplateDataDTO();
        remark.setValue("请前往首页-家居分期点击立即使用进行家居订单提交，如有疑问请联系客服。");

        Map<String, WechatMessageTemplateDataDTO> data = new HashMap<>();
        data.put("first", first);
        data.put("keyword1", keyword1);
        data.put("keyword2", keyword2);
        data.put("remark", remark);

        WechatMessageTemplateDTO wechatMessageTemplateDTO = new WechatMessageTemplateDTO();
        wechatMessageTemplateDTO.setOpneId(template.getOpenId());
        wechatMessageTemplateDTO.setTemplateId(WechatMessageTemplate.ORDER_DECISION_PASS.getTemplateId());
        wechatMessageTemplateDTO.setUrl(appHomePage);
        wechatMessageTemplateDTO.setData(data);

        //发送微信消息模板
        wechatService.sendTemplateMessage(wechatMessageTemplateDTO);

    }



}
